package com.machine.action.component;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.machine.bean.Model;
import com.machine.service.user.ComponentServiceDao;
import com.opensymphony.xwork2.ActionSupport;

@Component("componentAddAction")
@Scope(value = "prototype")
public class ComponentAddAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ComponentServiceDao componentServiceDao;
	private com.machine.bean.Componentpart component;
	private final static String UPLOADDIR = "/upload";
	private List<File> file;
	private List<String> fileFileName;
	private List<String> fileContentType;
	private Integer id;
	private List<Model> model_list;
	
	

	public List<Model> getModel_list() {
		return model_list;
	}

	public void setModel_list(List<Model> modelList) {
		model_list = modelList;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public List<File> getFile() {
		return file;
	}

	public void setFile(List<File> file) {
		this.file = file;
	}

	public List<String> getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}

	public List<String> getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}

	public ComponentServiceDao getComponentServiceDao() {
		return componentServiceDao;
	}

	@Resource(name = "componentService")
	public void setComponentServiceDao(ComponentServiceDao componentServiceDao) {
		this.componentServiceDao = componentServiceDao;
	}

	public com.machine.bean.Componentpart getComponent() {
		return component;
	}

	public void setComponent(com.machine.bean.Componentpart component) {
		this.component = component;
	}

	@SuppressWarnings("deprecation")
	private void uploadFile(int i) throws FileNotFoundException, IOException {
		try {
			InputStream in = new FileInputStream(file.get(i));
			String dir = ServletActionContext.getRequest().getRealPath(
					UPLOADDIR);
			File uploadFile = new File(dir, this.getFileFileName().get(i));
			if(i == 0){
				this.component.setDmodel(uploadFile.getPath().substring(uploadFile.getPath().indexOf("Machine")-1));
			}
			if(i == 1){
				this.component.setEngineeringdrawing(uploadFile.getPath().substring(uploadFile.getPath().indexOf("Machine")-1));
			}
			OutputStream out = new FileOutputStream(uploadFile);
			byte[] buffer = new byte[1024 * 1024];
			int length;
			while ((length = in.read(buffer)) > 0) {
				out.write(buffer, 0, length);
			}

			in.close();
			out.close();
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	@Override
	public String execute() throws Exception {

		if(file != null){
			
		for (int i = 0; i < file.size(); i++) {
			uploadFile(i);
		}
		
		}
		DateFormat   df   =   new   SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
		component.setOuttime(df.format(new Date()));
		component.setOutstate("普通");
		component.setComponentclass(1);
		id = componentServiceDao.add(this.component);
		
		model_list = componentServiceDao.findModel(component.getName(), 0, 10);
		ServletActionContext.getRequest().setAttribute("name", component.getName());
		return SUCCESS;
	}

}
